package com.xiaoshuidi.cloud.module.contract.controller.app.contract;

import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.ElectronicContractGenerateRespVO;
import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.ownerContract.ContractHouseOwnerPageReqVO;
import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.ownerContract.ContractHouseOwnerPageRespVO;
import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.ownerContract.ContractHouseOwnerRespVO;
import com.xiaoshuidi.cloud.module.contract.controller.app.contract.vo.OwnerHouseInfoRespVO;
import com.xiaoshuidi.cloud.module.contract.controller.app.contract.vo.OwnerHouseRentInfoRespVO;
import com.xiaoshuidi.cloud.module.contract.controller.app.contract.vo.OwnerHouseRoomQueryReqVO;
import com.xiaoshuidi.cloud.module.contract.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.contract.service.contract.ContractHouseOwnerService;
import com.xiaoshuidi.cloud.module.contract.service.contract.ElectronicContractService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

import static com.xiaoshuidi.cloud.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.error;
import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;

@Tag(name = "APP - 房东（业主）")
@RestController
@RequestMapping(ApiConstants.NAME + "/app/owner")
@Validated
public class AppOwnerController {
    @Resource
    private ContractHouseOwnerService houseOwnerService;
    @Resource
    private ElectronicContractService electronicContractService;

    /**
     * 获取房东房屋租金信息
     */
    @GetMapping("/get-rent-info")
    @Operation(summary = "获取房东房屋租金信息")
    public CommonResult<OwnerHouseRentInfoRespVO> getRentInfo() {
        try {
            Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
            if (Objects.isNull(loginUserId)) {
                throw exception(GlobalErrorCodeConstants.UNAUTHORIZED);
            }
            return success(houseOwnerService.getAppRentInfo(loginUserId));
        } catch (ServiceException e) {
            return error(e.getCode(),e.getMessage());
        }
    }

    /**
     * 分页查询房东业主房间信息
     */
    @GetMapping("/page-owner-room-info")
    @Operation(summary = "分页查询房东业主房间信息")
    public CommonResult<PageResult<OwnerHouseInfoRespVO>> pageOwnerRoomInfo(OwnerHouseRoomQueryReqVO param) {
        try {
            Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
            if (Objects.isNull(loginUserId)) {
                throw exception(GlobalErrorCodeConstants.UNAUTHORIZED);
            }
            param.setMemberId(loginUserId);
            return success(houseOwnerService.pageAppOwnerRoomInfo(param));
        } catch (ServiceException e) {
            return error(e.getCode(),e.getMessage());
        }
    }


    /**
     * 获得业主合同分页
     */
    @GetMapping("/page-owner-contract")
    @Operation(summary = "获得业主合同分页")
//    @PreAuthorize("@ss.hasPermission('contract:owner:query')")
    public CommonResult<PageResult<ContractHouseOwnerPageRespVO>> pageHouseOwner(@Valid ContractHouseOwnerPageReqVO param) {
        try {
            Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
            if (Objects.isNull(loginUserId)) {
                throw exception(GlobalErrorCodeConstants.UNAUTHORIZED);
            }
            param.setMemberId(loginUserId);
            return success(houseOwnerService.getHouseOwnerPage(param));
        } catch (ServiceException e) {
            return error(e.getCode(),e.getMessage());
        }
    }

    /**
     * 获得业主合同详情
     */
    @GetMapping("/get-owner-contract")
    @Operation(summary = "获得业主合同详情")
//    @PreAuthorize("@ss.hasPermission('contract:owner:query')")
    public CommonResult<ContractHouseOwnerRespVO> getHouseOwner(@RequestParam("id") Long id) {
        ContractHouseOwnerRespVO houseOwnerResp = houseOwnerService.getHouseOwnerResp(id);
        return success(houseOwnerResp);
    }

    /**
     * 获取电子合同（合同预览、下载）
     */
    @GetMapping("/get-own-electronic-contract")
    @Operation(summary = "获取电子合同")
    @Parameter(name = "contractId", description = "合同id", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('contract:generate')")
    public CommonResult<ElectronicContractGenerateRespVO> generateElectronicContract(@RequestParam("contractId") Long contractId) {
        try {
            return success(electronicContractService.generateOwnerElectronicContract(contractId));
        }  catch (ServiceException e) {
            return error(e.getCode(),e.getMessage());
        }
    }

}
